//
//  ClothStylesViewModel.swift
//  MyCloset
//
//  Created by 牟好好 on 2024/11/12.
//

import SwiftUI

class ClothStylesViewModel: ObservableObject {
    @Published var addedShangzhuang: [String] {
        didSet {
            UserDefaults.standard.set(addedShangzhuang, forKey: "userAddedShangzhuangStyles")
        }
    }
    
    @Published var addedXiazhuang: [String] {
        didSet {
            UserDefaults.standard.set(addedXiazhuang, forKey: "userAddedXiazhuangStyles")
        }
    }
    
    @Published var addedWaitao: [String] {
        didSet {
            UserDefaults.standard.set(addedWaitao, forKey: "userAddedWaitaoStyles")
        }
    }
    
    @Published var addedXiezi: [String] {
        didSet {
            UserDefaults.standard.set(addedXiezi, forKey: "userAddedXieziStyles")
        }
    }
    
    @Published var addedMaozi: [String] {
        didSet {
            UserDefaults.standard.set(addedMaozi, forKey: "userAddedMaoziStyles")
        }
    }
    
    @Published var recommendedCombinations: [[String]] {
        didSet {
            UserDefaults.standard.set(recommendedCombinations, forKey: "userRecommendedCombinations")
        }
    }
    
    // 合并后的样式数据
    var shangzhuang: [String] {
        return shangzhuangBase + addedShangzhuang
    }
    
    var xiazhuang: [String] {
        return xiazhuangBase + addedXiazhuang
    }
    
    var waitao: [String] {
        return waitaoBase + addedWaitao
    }
    
    var xiezi: [String] {
        return xieziBase + addedXiezi
    }
    
    var maozi: [String] {
        return maoziBase + addedMaozi
    }
    
    var allStyles: [String] {
        return shangzhuang + xiazhuang + waitao + xiezi + maozi + clothColors + clothTypes + clothThickness
    }
    
    var styleList: [[String]] {
        return [shangzhuang, xiazhuang, waitao, xiezi, maozi]
    }
    
    var addedStyleList: [[String]] {
        return [addedShangzhuang, addedXiazhuang, addedWaitao, addedXiezi, addedMaozi]
    }
    
    // 初始化
    init() {
        self.addedShangzhuang = UserDefaults.standard.array(forKey: "userAddedShangzhuangStyles") as? [String] ?? []
        self.addedXiazhuang = UserDefaults.standard.array(forKey: "userAddedXiazhuangStyles") as? [String] ?? []
        self.addedWaitao = UserDefaults.standard.array(forKey: "userAddedWaitaoStyles") as? [String] ?? []
        self.addedXiezi = UserDefaults.standard.array(forKey: "userAddedXieziStyles") as? [String] ?? []
        self.addedMaozi = UserDefaults.standard.array(forKey: "userAddedMaoziStyles") as? [String] ?? []
        self.recommendedCombinations = UserDefaults.standard.array(forKey: "userRecommendedCombinations") as? [[String]] ?? recommendedCombinationsBase
    }
    
    // 添加新样式
    func addStyle(to category: String, newStyle: String) -> Bool {
        guard !allStyles.contains(newStyle) && newStyle != "" else { return false }
        // 根据不同类别来添加样式
        switch category {
        case "上装":
            if !addedShangzhuang.contains(newStyle) && !shangzhuang.contains(newStyle) {
                addedShangzhuang.append(newStyle)
            }
        case "下装":
            if !addedXiazhuang.contains(newStyle) && !xiazhuang.contains(newStyle) {
                addedXiazhuang.append(newStyle)
            }
        case "外套":
            if !addedWaitao.contains(newStyle) && !waitao.contains(newStyle) {
                addedWaitao.append(newStyle)
            }
        case "鞋子":
            if !addedXiezi.contains(newStyle) && !xiezi.contains(newStyle) {
                addedXiezi.append(newStyle)
            }
        case "帽子":
            if !addedMaozi.contains(newStyle) && !maozi.contains(newStyle) {
                addedMaozi.append(newStyle)
            }
        default:
            break
        }
        return true
    }
    
    // 删除样式
    func removeStyle(from category: String, styleToRemove: String, completion: @escaping () -> Void) {
        // 根据不同类别来删除样式
        switch category {
        case "上装":
            if let index = addedShangzhuang.firstIndex(of: styleToRemove) {
                addedShangzhuang.remove(at: index)
            }
        case "下装":
            if let index = addedXiazhuang.firstIndex(of: styleToRemove) {
                addedXiazhuang.remove(at: index)
            }
        case "外套":
            if let index = addedWaitao.firstIndex(of: styleToRemove) {
                addedWaitao.remove(at: index)
            }
        case "鞋子":
            if let index = addedXiezi.firstIndex(of: styleToRemove) {
                addedXiezi.remove(at: index)
            }
        case "帽子":
            if let index = addedMaozi.firstIndex(of: styleToRemove) {
                addedMaozi.remove(at: index)
            }
        default:
            break
        }
        completion()
    }
    
    // 添加搭配组合的方法
    func addCombination(_ newCombination: [String]) {
        // 确保新搭配组合不重复
        if !recommendedCombinations.contains(where: { $0.sorted() == newCombination.sorted() }) {
            recommendedCombinations.append(newCombination)
        }
    }
    
    // 删除搭配组合的方法
    func removeCombination(_ combinationToRemove: [String]) {
        if let index = recommendedCombinations.firstIndex(where: { $0 == combinationToRemove }) {
            recommendedCombinations.remove(at: index)
        }
    }
}
